from flask import Blueprint, request, jsonify
from app.services import AuthService
from flask_cors import cross_origin
bp = Blueprint('auth', __name__)

@bp.route('/register', methods=['POST'])
@cross_origin()
def register():
    """用户注册"""
    data = request.get_json()
    # 打印
    print('+++++++++++++++++++++++++++++++++++++++++++++++++')
    print(data)
    print('+++++++++++++++++++++++++++++++++++++++++++++++++')
    result = AuthService.register(
        username=data.get('username'),
        password=data.get('password'),
        role=None
    )
    return jsonify(result), result['code']

@bp.route('/login', methods=['POST'])
@cross_origin()
def login():
    """用户登录"""
    data = request.get_json()
    print('+++++++++++++++++++++++++++++++++++++++++++++++++')
    print(data)
    print('+++++++++++++++++++++++++++++++++++++++++++++++++')
    result = AuthService.login(
        username=data.get('username'),
        password=data.get('password')
    )
    return jsonify(result), result['code']

@bp.route('/reset-password', methods=['POST'])
@cross_origin()
def reset_password():
    """密码重置"""
    data = request.get_json()
    result = AuthService.reset_password(
        user_id=data.get('user_id'),
        new_password=data.get('new_password')
    )
    return jsonify(result), result['code']